package lowijs.XfDroid;

import java.text.*;
import java.util.*;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;

import lowijs.XfireClient.*;
import lowijs.XfireClient.event.*;

public class ConvoWindow extends Activity implements XfireObserver {
	private Conversation cv = null;
	private ConvoWindowAdapter adapter = null;
	private ListView lv = null;
	
	public void onCreate(Bundle b) {
		super.onCreate(b);
		setContentView(R.layout.convowindow);
		
		byte[] userid = getIntent().getExtras().getByteArray("friend");
		Friend f = FriendManager.getInstance().getFriendByUserid(userid);
		cv = ConversationManager.getInstance().getConversation(f);
		
		adapter = new ConvoWindowAdapter(this, R.layout.convoline, cv.getMessages());
		lv = (ListView)findViewById(R.id.lvConversation);
		lv.setAdapter(adapter);
		
		View.OnClickListener sendListener = new View.OnClickListener() {
			public void onClick(View v) {
				sendIM();
			}
		};
		((Button)findViewById(R.id.btSend)).setOnClickListener(sendListener);
		
		View.OnKeyListener returnListener = new View.OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN
						&& keyCode == KeyEvent.KEYCODE_ENTER) {
					sendIM();
					return true;
				}
				return false;
			}
		};
		
		EditText etMessage = (EditText)findViewById(R.id.etMessage);
		etMessage.setOnKeyListener(returnListener);
		etMessage.requestFocus();
	}
	
	public void onStart() {
		super.onStart();
		EventManager.addObserver(this);
		convoHandler.sendEmptyMessage(0);
		XfDroidService.sendNoNotificationsFor(cv);
	}
	
	public void onPause() {
		super.onPause();
		XfDroidService.sendAllNotifications();
	}
	
	public void onStop() {
		super.onStop();
		EventManager.removeObserver(this);
	}
	
	private void sendIM() {
		EditText etMessage = (EditText)findViewById(R.id.etMessage);
		cv.sendIM(etMessage.getText().toString());
		
		etMessage.setText("");
	}
	
	public void handleEvent(XfireEvent event) {
		switch(event.getEventType()) {
		case XfireEvent.XF_RECVIM:
			ReceiveImEvent e = (ReceiveImEvent)event;
			
			if (e.getConversation() != cv)
				return;
		case XfireEvent.XF_SENDIM:
			convoHandler.sendEmptyMessage(0);
			return;
		case XfireEvent.XF_DCREASON:
			Intent i = new Intent(this, Login.class);
			i.putExtra("disconnected", true);
			startActivity(i);
			finish();
			return;
		}
	}
	
	private Handler convoHandler = new Handler() {
		public void handleMessage(Message m) {
			adapter.notifyDataSetChanged();
			lv.setSelection(lv.getCount() - 1);
		}
	};
	
	private class ConvoWindowAdapter extends ArrayAdapter<IM> {
		public ConvoWindowAdapter(Context context, int textViewResourceId,
				List<IM> objects) {
			super(context, textViewResourceId, objects);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
            if (v == null) {
                LayoutInflater li = (LayoutInflater)getSystemService(
                                Context.LAYOUT_INFLATER_SERVICE);
                v = li.inflate(R.layout.convoline, null);
            }

            IM im = getItem(position);
            
            TextView tvTime = (TextView)v.findViewById(R.id.tvTime);
            TextView tvNickname = (TextView)v.findViewById(R.id.tvNickname);
            TextView tvMessage = (TextView)v.findViewById(R.id.tvMessage);
            
            SimpleDateFormat date = new SimpleDateFormat("HH:mm");
            
            tvTime.setText("[" + date.format(im.getDate()) + "]");
            tvNickname.setText(im.getName());
            tvMessage.setText(im.getMessage());
            
            return v;
		}
	}
}
